{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5b910baa",
   "metadata": {},
   "source": [
    "# 知识点_列表基本方法(列表的私有方法)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5f244732",
   "metadata": {},
   "outputs": [],
   "source": [
    "# anthor:huanmin\n",
    "# 时间：week06\n",
    "names = [\"huanmin\", \"yuyan\", \"yanzu\", \"kuli\"]\n",
    "stu_id = [101, 102, 103, 104, 101, 106, 101, 109, 101]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f5d1acb",
   "metadata": {},
   "source": [
    "###### 增加的方法：\n",
    "* append() [增加在list的末尾]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "70235ce9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "append之后的names: ['huanmin', 'yuyan', 'yanzu', 'kuli', 'yuning']\n"
     ]
    }
   ],
   "source": [
    "names.append(\"yuning\")\n",
    "print(\"append之后的names:\", names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93bee326",
   "metadata": {},
   "source": [
    "* insert() [可以指定位置增加元素]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8df620e4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "insert之后的names： ['huanmin', 'pp', 'yuyan', 'yanzu', 'kuli', 'yuning']\n"
     ]
    }
   ],
   "source": [
    "names.insert(1, \"pp\")          # 表示在1这个位置加“pp”\n",
    "print(\"insert之后的names：\", names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a8af441",
   "metadata": {},
   "source": [
    "###### 删除的方法：[如果删除list中的多个元素时，remove比较合适；pop可删除特定位置的元素]\n",
    "* remove() [一般情况下，使用remove先用in做判断]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8d912e9e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remove删除的结果： [102, 103, 104, 101, 106, 101, 109, 101]\n"
     ]
    }
   ],
   "source": [
    "if 101 in stu_id:\n",
    "    stu_id.remove(101)\n",
    "print(\"remove删除的结果：\", stu_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a8436c4",
   "metadata": {},
   "source": [
    "* pop() [删除位置为2的元素]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b0591c1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pop()方法后的结果: [102, 103, 101, 106, 101, 109, 101]\n"
     ]
    }
   ],
   "source": [
    "stu_id.pop(2)\n",
    "print(\"pop()方法后的结果:\", stu_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40d5e31d",
   "metadata": {},
   "source": [
    "###### index（）索引 [查看该元素如101在stu_id中第一个出现的位置索引]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9d2f3fb5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "print(stu_id.index(101))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4d3ac9e",
   "metadata": {},
   "source": [
    "###### clear() [清除所有数据]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9e88771e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "清除names列表后的值： []\n"
     ]
    }
   ],
   "source": [
    "names.clear()\n",
    "print(\"清除names列表后的值：\", names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "884005e0",
   "metadata": {},
   "source": [
    "###### count() [查看列表元素出现的个数，（简单应用）可以应用于统计分数出现的次数]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "99455716",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "count方法的结果： 3\n",
      "[102, 103, 101, 106, 101, 109, 101]\n"
     ]
    }
   ],
   "source": [
    "print(\"count方法的结果：\", stu_id.count(101))\n",
    "print(stu_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "951476bc",
   "metadata": {},
   "source": [
    "###### extend() [给指定的list进行扩充]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5647796d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[102, 103, 101, 106, 101, 109, 101]\n"
     ]
    }
   ],
   "source": [
    "names.extend(stu_id)\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f7185fa",
   "metadata": {},
   "source": [
    "###### 列表的其他方法（Sequence Types 的一些方法）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "075692d6",
   "metadata": {},
   "source": [
    "###### len() [查看list长度]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f7c1538c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "names的长度/names的列表元素个数： 7\n"
     ]
    }
   ],
   "source": [
    "print(\"names的长度/names的列表元素个数：\",len(names))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17c0372c",
   "metadata": {},
   "source": [
    "###### in [查看元素是否存在list当中，结果为布尔值，False/True,一般在操作List的修改和删除时会先判断]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7f6f8b58",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(109 in names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbacfa07",
   "metadata": {},
   "source": [
    "# 知识点_列表高级方法_枚举"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "03a2f37a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n",
      "\n",
      "0 101\n",
      "1 102\n",
      "2 103\n",
      "3 104\n",
      "4 101\n",
      "5 106\n",
      "6 101\n",
      "7 109\n",
      "8 101\n"
     ]
    }
   ],
   "source": [
    "stu_id = [101, 102, 103, 104, 101, 106, 101, 109, 101]\n",
    "# 目的：取出所有位置101的index\n",
    "for i in stu_id:\n",
    "    # 循环遍历stu_id的所有内容\n",
    "    if i == 101:\n",
    "        # 判断如果元素为101的时候,怎么取出索引值？\n",
    "        print()\n",
    "# 枚举:因为list其实不仅有values值，还有index索引，但for循环主要循环遍历其值，不遍历索引\n",
    "# 因此，有了枚举的方法，同时遍历两者\n",
    "# enumerate()\n",
    "pop_values_list = []  # append方法经常用于新建列表\n",
    "for index,item in enumerate(stu_id):\n",
    "    print(index,item)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e5f6d29d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8 101\n",
      "我们想要删除pop()的值： [8]\n"
     ]
    }
   ],
   "source": [
    "if item == 101:\n",
    "        print(index,item)\n",
    "        pop_values_list.append(index)\n",
    "print(\"我们想要删除pop()的值：\",pop_values_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7e8d512",
   "metadata": {},
   "source": [
    "# 知识点_列表高级方法_copy（）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a83027a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [101, 102, 103, 104, 101, 106, 101, 109, 101] \n",
      " stu_id_matedata: [101, 102, 103, 104, 101, 106, 101, 109, 101]\n"
     ]
    }
   ],
   "source": [
    "stu_id = [101, 102, 103, 104, 101, 106, 101, 109, 101]\n",
    "#  copy()很重要\n",
    "# 思考，赋值是真正的复制么？ 赋值(=) <=> copy? 不等价\n",
    "stu_id_matedata = stu_id\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10e643a2",
   "metadata": {},
   "source": [
    "######  实验 1 赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b0316b6c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [101, 103, 104, 101, 106, 101, 109, 101] \n",
      " stu_id_matedata: [101, 103, 104, 101, 106, 101, 109, 101]\n"
     ]
    }
   ],
   "source": [
    "stu_id = [101, 102, 103, 104, 101, 106, 101, 109, 101]\n",
    "stu_id_matedata = stu_id\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "217e983d",
   "metadata": {},
   "source": [
    "###### 实验2 copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "de0f5f10",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [101, 104, 101, 106, 101, 109, 101] \n",
      " stu_id_matedata: [101, 103, 104, 101, 106, 101, 109, 101]\n"
     ]
    }
   ],
   "source": [
    "stu_id_matedata = stu_id.copy()\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd68390b",
   "metadata": {},
   "source": [
    "###### 实验3 切片实现复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1c3d46fa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [101, 101, 106, 101, 109, 101] \n",
      " stu_id_matedata: [101, 104, 101, 106, 101, 109, 101]\n"
     ]
    }
   ],
   "source": [
    "stu_id_matedata = stu_id[:]\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4eced3b",
   "metadata": {},
   "source": [
    "# 练习"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "733f87ff",
   "metadata": {},
   "source": [
    "* 列表还支持连接等操作："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "facba4a7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares = [1, 4, 9, 16, 25]\n",
    "squares + [36, 49, 64, 81, 100] # 两个列表链接在一起"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43343e40",
   "metadata": {},
   "source": [
    "* 与不可变的字符串不同，列表是可变 类型，即可以更改其内容："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b4483c12",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 8, 27, 64, 125]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hm = [1, 8, 27, 65, 125]\n",
    "hm[3] = 64\n",
    "hm"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68384304",
   "metadata": {},
   "source": [
    "* append() 方法在列表末尾添加新项目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3a19cad9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 8, 27, 64, 125, 216, 343, 216, 343, 216, 343, 216, 343]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hm.append(216)  \n",
    "hm.append(7 ** 3)  \n",
    "hm"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83e6d9ca",
   "metadata": {},
   "source": [
    "* 可以嵌套列表（创建包含其他列表的列表）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ccbf7c5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['a', 'b', 'c'], [1, 2, 3]]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = ['a', 'b', 'c']\n",
    "n = [1, 2, 3]\n",
    "x = [a, n]\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "67c89e33",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b', 'c']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c765cf18",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'b'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[0][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "26e43219",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The value of i is 65536\n"
     ]
    }
   ],
   "source": [
    "i = 256*256\n",
    "print('The value of i is', i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "189ad5bb",
   "metadata": {},
   "source": [
    "## 列表汇总"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d924ddb6",
   "metadata": {},
   "source": [
    "|列表名称    | 作用        | \n",
    "|:-----------:| -------------:|\n",
    "| append( x )  | 将一个项目添加到列表的末尾  | \n",
    "|extend（可迭代） |  通过附加迭代中的所有项目来扩展列表 |\n",
    "|insert( i , x )   | 在给定位置插入一个项目 | \n",
    "| remove( x )     | 从列表中删除值等于x的第一项 | \n",
    "| pop( [我] )     |  删除列表中给定位置的项目，并返回它| \n",
    "| clear( )     | 从列表中删除所有项目 | \n",
    "|  index( x [ ,开始[ ,结束] ] )    | 在值等于x的第一个项目的列表中返回从零开始的索引 | \n",
    "|  count( x )    | 返回x在列表中出现的次数 | \n",
    "|  sort( * , key = None , reverse = False )    | 对列表中的项目进行排序 | \n",
    "|  reverse( )    | 将列表的元素原地反转 | \n",
    "|  copy( )  | 返回列表的浅拷贝 | \n",
    "|:-----------:| -------------:|\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0b3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
